﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CadStrategy2013;
using CadStrategy2013.CgqStyle;
using MyCadDll;
using System.IO;
using XhPaper.功能实现;
using MyIni;
using System.Threading;

namespace cad2013
{
    public partial class Form1 : Form
    {
        private CommuCfgForm comCfgFrm;
        public Form1()
        {
            comCfgFrm = new CommuCfgForm();
            if (DialogResult.OK == comCfgFrm.ShowDialog())
            {

            }
            try
            {
                //if(File.Exists("c:\\windows0\\log.dat")==false)
                //    File.Create("c:\\windows0\\log.dat");
                //IniFile f = new IniFile("c:\\windows0\\log.dat");
                //f.Load();
              
                //string path = Application.StartupPath;
                //if (File.Exists(path + "\\l.dat"))
                //{
                //    f.WriteInt("cad", "n", 0);
                //    File.Delete(path + "\\l.dat");
                //}
                //f.WriteString("cad", "d", path);
                //int n = f.GetInt("cad", "n", 0);
                //int t = DateTime.Now.Year;
                //n++;
                ////if (n > 20)
                ////{
                ////    if (File.Exists(path + "\\template"))
                ////        File.Delete(path + "\\template");
                ////    return;
                ////}
                //f.WriteInt("cad", "n", n);
                //f.Save();
                InitializeComponent();
            }
            catch(Exception e)
            {
               // MessageBox.Show(e.ToString());
            }
            comCfgFrm.ReadStatisticsFile();
        }
        public void strategy_DealDataEvent(object sender, EventArgs e)
        {
            //if (progressBar1.Value >= progressBar1.Maximum)
            //{
            //    progressBar1.Value = 1;
            //}
            //progressBar1.Maximum = ((CadStrategy2013.DxfData.WriteDxfArgs)e).Total;
            //progressBar1.Step = 1;
            //progressBar1.PerformStep();
            ShowProgressBarDelegate showProgress = new ShowProgressBarDelegate(ShowProgressBar);
            this.Invoke(showProgress, new object[] { ((CadStrategy2013.DxfData.WriteDxfArgs)e).Total });
        }
        delegate void ShowProgressBarDelegate(int total);
        private void ShowProgressBar(int total)
        {
            if (progressBar1.Value >= progressBar1.Maximum)
            {
                progressBar1.Value = 1;
            }
            progressBar1.Maximum = total;
            progressBar1.Step = 1;
            progressBar1.PerformStep();
        }

        private void WriteDxf(Cgq2013 strategy)
        {
            Thread thread = new Thread(new ThreadStart(strategy.WriteDxf));
            thread.Start();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new DsDlClass("灯丝电流");
            strategy.DealDataEvent+=new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new BzdZlClass("半自动",comCfgFrm.bzdDic,comCfgFrm.zlDic);
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new DmhClass("站内电码化",comCfgFrm.dmhDic);
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new ZLBsClass("直流道岔表示",comCfgFrm.zldcbsDic);
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new ZLDcClass("直流道岔电流",comCfgFrm.zldcdlDic);
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new YpFsClass("无绝缘发送",comCfgFrm.ypfsDic);
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new YpJsClass("无绝缘接收",comCfgFrm.ypjsDic);
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new GdClass("模入板");
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            //MyCad cad = new MyCad();
            //cad.StartCad();
           // cad.ReadSelectSetToIni("jy");
            Chart chart = new Chart();
            DataTable dt = chart.ConvertDataTable();
            ReadCadDataForm form = new ReadCadDataForm(dt);
            form.ShowDialog();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new JyDyClass("绝缘");
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDwg();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            JySeledtForm form = new JySeledtForm();
            if (form.ShowDialog() == DialogResult.OK)
            {
                if (form.ZuHeStyle != "" && form.SavePath != "")
                {
                    JyDyClass jy = new JyDyClass("绝缘");
                    jy.GetCfg();
                    jy.GetJyFile(form.ZuHeStyle, form.SavePath,form.IsFuJia);
                }
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new KgbClass("开入板");
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            IniFile inifile = new IniFile("./\\全局.ini");
            string stationname = inifile.GetString("全局", "站名", "");
            Dyhx dyhx= new Dyhx("./\\" + stationname + "\\dyhx.ini");
            MyCad cad = new MyCad();
            cad.StartCad();
            dyhx.MyDraw(cad);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            IniFile inifile=new IniFile("./\\全局.ini");
            string stationname=inifile.GetString("全局","站名","");
            DataHx datahx = new DataHx("./\\" + stationname + "\\datahx.ini");
            MyCad cad = new MyCad();
            cad.StartCad();
           // cad.ReplaceText("2/4", "dd");
            datahx.MyDraw(cad);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new JLBsClass("交流道岔表示",comCfgFrm.jldcbsDic);
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new JLDcClass("交流道岔电流",comCfgFrm.jldcdlDic);
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button18_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new Cgq2013("");
            strategy.CopyCommonFile();
            MessageBox.Show("ok");
        }

        private void button19_Click(object sender, EventArgs e)
        {
            IniFile inifile = new IniFile("./\\全局.ini");
            string stationname = inifile.GetString("全局", "站名", "");
            HuanXianForm2 hxFrm = new HuanXianForm2("./\\" + stationname,"v1");
            hxFrm.ShowDialog();
        }

        private void GetDataFromGdb_Click(object sender, EventArgs e)
        {
            IniFile inifile = new IniFile("./\\全局.ini");
            string stationname = inifile.GetString("全局", "站名", "");
            string path=inifile.Path.Substring(0,inifile.Path.LastIndexOf("\\"));
            IbForm form = new IbForm();
            form.ShowDialog();
            //DataForm form = new DataForm(path + "\\" + stationname);
            //form.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Cgq2013 strategy = new GyBdcClass("高压不对称", comCfgFrm.gybdcDic);
            strategy.DealDataEvent += new EventHandler(strategy_DealDataEvent);
            strategy.WriteDxfEvent += new EventHandler(strategy_DealDataEvent);
            WriteDxf(strategy);
        }

        private void button20_Click(object sender, EventArgs e)
        {
            MyCad cad = new MyCad("./\\jy.ini");
            cad.StartCad();
            cad.ReadSelectSetToIni();
        }
    }
}
